/*
 * Copyright (c) 2020, chenshuai (cs4380@163.com).
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cs.springcloud.admin.server.biz;

import com.cs.springcloud.admin.api.model.SysTenant;
import com.cs.springcloud.admin.server.mapper.SysTenantMapper;
import com.cs.springcloud.common.biz.BaseBusinessBiz;
import com.cs.springcloud.common.constant.CommonConstant;
import com.cs.springcloud.common.constant.HttpStatusConstant;
import com.cs.springcloud.common.exception.BusinessException;
import com.cs.springcloud.common.util.BeanUtil;
import com.cs.springcloud.common.util.UUIDUtil;
import com.cs.springcloud.common.vo.ParamQuery;
import org.springframework.stereotype.Service;

/**
 * SysTenant 基础租户表.
 *
 * @author cs
 * @version 1.0
 * @date 2019-10-02 10:01:49
 */
@Service
public class SysTenantBiz extends BaseBusinessBiz<SysTenantMapper, SysTenant> {
    /**
     * 添加租户
     *
     * @param entity 实体
     */
    @Override
    public int insertModel(SysTenant entity) {
        BeanUtil.beanAttributeValueTrim(entity);
        entity.setId(UUIDUtil.randomUUID());
        return super.insertModel(entity);
    }

    @Override
    public SysTenant selectOne(ParamQuery query) {
        return super.selectOne(query);
    }

    /**
     * 逻辑删除
     *
     * @param id 主键id
     */
    @Override
    public int deleteById(Object id) {
        if (null == id) {
            throw new BusinessException("请选择待删除对象！", HttpStatusConstant.FAIL);
        }
        SysTenant sysTenant = new SysTenant();
        sysTenant.setId(id.toString());
        sysTenant.setIsDeleted(CommonConstant.IS_DELETED);
        return super.updateById(sysTenant);
    }

    /**
     * TODO 测试分布式事务添加租户
     * <p>
     *     可移除
     * </p>
     */
    public void testTransaction(){
        SysTenant sysTenant = new SysTenant();
        sysTenant.setId(UUIDUtil.randomUUID());
        sysTenant.setTenantCode(UUIDUtil.randomUUID());
        sysTenant.setTenantName("123456");
        this.insertModel(sysTenant);
        // throw new RuntimeException("数据库异常");
    }
}